package com.itguigu.spzx.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.itguigu.spzx.common.entity.LoginUser;
import com.itguigu.spzx.common.utils.ThreadLocalUtil;
import com.itguigu.spzx.model.entity.User;
import com.itguigu.spzx.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/api/user")
@Slf4j
@RefreshScope
public class UserController {

    @Autowired
    private UserService userService ;
    @Value("${key}")
    public String value;

    @GetMapping(value = "/findUserByUserId/{userId}")
    public User findUserByUserId(@PathVariable(value = "userId") Long userId) {
        LoginUser loginUser = ThreadLocalUtil.threadLocal.get();
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.debug(loginUser.getUserId());
        return userService.findUserByUserId(userId) ;
    }

    @GetMapping("/test1")
    public String test1(){
        System.out.println(value);
        return "Success Test1";
    }

    @SentinelResource("users")
    @GetMapping("/test2")
    public String test2(){
        userService.getUserById();
        System.out.println("test2");
        return "Success Test2";
    }

    @GetMapping("/test3")
    public String test3(){
        userService.getUserById();
        System.out.println("test3");
        return "Success Test3";
    }

}
